'GCV_FRONT = 2
'GCV_LEFT = 4
'GCV_RIGHT = 5
'GCV_EXTENDED = 7
'GCV_SIMPLIFIED = 8
'GCV_STANDARD = 9
'SIDE = GCV_FRONT

'DETAIL_LEVEL =GCV_EXTENDED

'        PAR_din   = 25
'        PAR_D   = 52
'        PAR_B   = 30
'        PAR_C   = 25.4
'        PAR1_dain = (30+30.5) * 0.5
'        PAR1_Da = 47
'        PAR1_Dw = 7.94
'        PAR1_r  =  1.5

'        PAR2_r1 =  0.5
        
'        PAR2_tip =4 ' tip O = PAR3_tip=3 ; tip X = PAR3_tip=4; tip T AR3_tip=1 ;


hatchAngle  = 45                  
'hatchStep= 2                   

drawFace1 =1

'+++++++        
Dim math,iDA,iPoint
Const POINT_ARR = 2  
Const ko_MathPointParam = 14

 ''PAR_Simpl_AXIS = 1
''PAR_AXIS = 1
'++++
SUB LINE (x1,y1,x2,y2,t) Doc.ksLineSEG x1,y1,x2,y2,t END SUB
SUB Circle (x1,y1,r,t) Doc.ksCircle x1,y1,r,t END SUB
'++++
SUB DrawBall (xtr, ytr, rads, del)
  Line  xtr-rads-del,ytr,xtr+rads+del,ytr,3
  Line  xtr,ytr+rads+del,xtr,ytr-rads-del,3
  Circle xtr,ytr,rads,1
end sub
'++++
'++++
SUB Contur (rn, rf1, rf2,rf3, rf4, b,  ky) 
 x1=rf1
 x2=rf2
 x3=b-rf3
 x4=b-rf4
 y2=(rn-rf2)*ky
 y3=(rn-rf3)*ky
  doc.ksArcByPoint x2,y2,rf2,0,y2,x2,rn*ky,-ky,1 ';  /*5*/
  line x2,rn*ky,x3,rn*ky,1
 doc.ksArcByPoint x3,y3,rf3,x3,rn*ky,b,y3,-ky,1 ' ; /*6*/
 Line 0, y2, 0,0,1
 Line b,y3,b,0,1
END SUB
'++++
SUB Apx (rn)
  doc.ksColouring &HFE000000
Contur rn, PAR1_r, PAR1_r, PAR1_r,  PAR1_r, PAR_B/2, 1
Contur rn, PAR1_r, PAR1_r, PAR1_r,  PAR1_r, PAR_B/2, -1
doc.ksEndObj
END SUB
'+++++++++

SUB Script_OnLoad ()

Set math = Kompas.GetMathematic2D
Set iDA = Kompas.GetDynamicArray(POINT_ARR)
Set iPoint = Kompas.GetParamStruct(ko_MathPointParam)

   ys=( PAR_din+PAR_D)*0.25
   xs=PAR_B*0.25
   rn=PAR_D*0.5
   rv=PAR_din*0.5
   rv2=PAR1_dain*0.5
   rn2=PAR1_Da*0.5
   rsh = PAR1_Dw*0.5
   B = PAR_B*0.5

  SELECT CASE PAR2_tip 
    case 1:
      flag1 = 1
      flag2 = 1
    case 2:
      flag1 = 1
      flag2 = 1
    case 3:
      flag1 = 0
      flag2 = 1
    case 4:
      flag1 = 1
      flag2 = 0
END SELECT

  if rv2 < (ys-0.8*rsh) then
    rv2 = ys-0.8*rsh
end if
  if  rn2 > (ys+0.8*rsh) then
    rn2 = ys+0.8*rsh
end if
  
h =0'PAR_B
ang = 0'180
Doc.ksMtr h, 0, ang, 1 ,1
APX rn
Doc.KsMtr B, 0, 0, 1,1 
APX rn
Doc.KsDeleteMtr 

SELECT CASE DETAIL_LEVEL
      Case GCV_EXTENDED:

'hatchAngle = hatchAngle+90
   DrawOutsideRing   rn,  rn2,  B,  PAR1_r, xs, ys, rsh, 1
        DrawBall  xs, ys, rsh,  2
       DrawInsideRing   rv, rv2, B,  PAR1_r, xs, ys, rsh, 1,  PAR2_r1, flag1  
         


Par0 = rn-PAR1_r
Par1 =  rn-PAR1_r


    If PAR_AXIS <> 1 Then 
      line  0, Par0, 0, rv+PAR1_r, 1 
      line  B, Par1, B, rv+PAR2_r1, 1 
    End If         

        DrawOutsideRing   rn,  rn2,  B,  PAR1_r, xs, ys, rsh, -1
        DrawBall  xs, -ys,  PAR1_Dw*0.5,  2
       DrawInsideRing   rv, rv2,  B,  PAR1_r, xs, ys, rsh, -1,  PAR2_r1, flag1 


Par0 =  -(rn-PAR1_r)

Par1=-(rn-PAR1_r)


     If PAR_AXIS <> 1 Then 
        line  0, Par0, 0, -(rv+PAR1_r), 1 
        line  B, Par1, B, -(rv+PAR2_r1), 1 
      End If 
hatchAngle = hatchAngle-90
Doc.KsMtr B, 0, 0, 1,1 
'hatchAngle = hatchAngle+90



     DrawOutsideRing   rn,  rn2,  B,  PAR1_r, xs, ys, rsh, 1 
       DrawBall  xs, ys, rsh,  2
      DrawInsideRing   rv, rv2,  B,  PAR1_r, xs, ys, rsh, 1,  PAR2_r1, flag2  



Par2 = rn-PAR1_r
         
 line  B, Par2, B, rv+PAR1_r, 1 

        DrawOutsideRing   rn,  rn2,  B,  PAR1_r, xs, ys, rsh, -1
        DrawBall  xs, -ys,  PAR1_Dw*0.5,  2
       DrawInsideRing   rv, rv2,  B,  PAR1_r, xs, ys, rsh, -1,  PAR2_r1, flag2  

Par2 =-( rn-PAR1_r)
line  B, Par2, B, -(rv+PAR1_r), 1 
           
Doc.KsDeleteMtr 

case  GCV_STANDARD:
' hatchAngle = hatchAngle+90
     DrawOutsideRing   rn,  rn2,  B,  PAR1_r, xs, ys, rsh, 1 
          DrawBall  xs, ys, rsh,  2
          DrawInsideRing   rv, rv2,  B,  PAR1_r, xs, ys, rsh, 1,  PAR2_r1, flag1  

Par0 = rn-PAR1_r
Par1 =  rn-PAR1_r
   If PAR_AXIS <> 1 Then 
            line  0, Par0, 0, rv+PAR1_r, 1 
            line  B, Par1,B, rv+PAR2_r1, 1 
   End If 

P_r1 = PAR1_r
P_r2 = PAR1_r

if flag1=1 and flag2=1 then
DrawSimple  rv, rn,  PAR1_r,  PAR1_r, PAR1_r , PAR2_r1 , B, -1 
end if

if flag1=0 and flag2=1 then
DrawSimple  rv, rn,  PAR2_r1,  PAR1_r, PAR1_r , PAR1_r , B, -1 
end if
if flag1=1 and flag2=0 then
DrawSimple  rv, rn,  PAR1_r,  PAR1_r, PAR1_r , PAR2_r1 , B, -1 
end if

hatchAngle = hatchAngle-90
           Doc.KsMtr B, 0,0 , 1,1 
 
          DrawOutsideRing   rn,  rn2,  B,  PAR1_r, xs, ys, rsh, 1 
          DrawBall  xs, ys, rsh,  2
          DrawInsideRing   rv, rv2,  B,  PAR1_r, xs, ys, rsh, 1,  PAR2_r1, flag2  

'  //             line ( 0, rn-PAR1_r, 0, rv+PAR1_r, 1 );



Par2 = rn-PAR1_r
            
   If PAR_AXIS <> 1 Then       
       line  B, Par2, B, rv+PAR1_r, 1              
   End If 

P_r1 = PAR1_r
P_r2 = PAR1_r


if flag1=1 and flag2=1 then
DrawSimple  rv, rn,  PAR1_r,  PAR1_r, PAR1_r , PAR2_r1 , B, -1 
end if
if flag1=0 and flag2=1 then
DrawSimple  rv, rn,  PAR1_r,  PAR1_r, PAR1_r , PAR2_r1 , B, -1 
end if
if flag1=1 and flag2=0 then
DrawSimple  rv, rn,  PAR2_r1,  PAR1_r, PAR1_r , PAR1_r , B, -1 
end if


 Doc.KsDeleteMtr

  CASE GCV_SIMPLIFIED:

P_r1 = PAR1_r
P_r2 = PAR1_r

if flag1=1 and flag2=1 then
DrawSimple  rv, rn,  PAR1_r,  PAR1_r, PAR1_r , PAR2_r1 , B, 1 
DrawSimple  rv, rn,  PAR1_r,  PAR1_r, PAR1_r , PAR2_r1 , B, -1 
end if
if flag1=0 and flag2=1 then
DrawSimple  rv, rn,  PAR2_r1,  PAR1_r, PAR1_r , PAR1_r , B, 1 
DrawSimple  rv, rn,  PAR2_r1,  PAR1_r, PAR1_r , PAR1_r , B, -1 
end if
if flag1=1 and flag2=0 then
DrawSimple  rv, rn,  PAR1_r,  PAR1_r, PAR1_r , PAR2_r1 , B, 1 
DrawSimple  rv, rn,  PAR1_r,  PAR1_r, PAR1_r , PAR2_r1 , B, -1 
end if


P_r1 = PAR1_r
P_r2 = PAR1_r

Doc.KsMtr B, 0,0, 1,1 

if flag1=1 and flag2=1 then
DrawSimple  rv, rn,  PAR1_r,  PAR1_r, PAR1_r , PAR2_r1 , B, 1 
DrawSimple  rv, rn,  PAR1_r,  PAR1_r, PAR1_r , PAR2_r1 , B, -1 
end if
if flag1=0 and flag2=1 then
DrawSimple  rv, rn,  PAR1_r,  PAR1_r, PAR1_r , PAR2_r1 , B, 1 
DrawSimple  rv, rn,  PAR1_r,  PAR1_r, PAR1_r , PAR2_r1 , B, -1 
end if
if flag1=1 and flag2=0 then
DrawSimple  rv, rn,  PAR2_r1,  PAR1_r, PAR1_r , PAR1_r , B, 1 
DrawSimple  rv, rn,  PAR2_r1,  PAR1_r, PAR1_r , PAR1_r , B, -1 
end if

Doc.KsDeleteMtr 
    END SELECT
     
Doc.KsDeleteMtr 
hatchAngle  = 45                  

End Sub
'+++++

'++++++++++++++++++
SUB DrawOutsideRing  ( rn,   rn2,  b,  r,  xs, ys,  rads,  ky)  
 
Dim  xp(2)

Y3=(rn-r)*ky

iDA.ksClearArray
 math.ksIntersectLinsCir 0,rn2*ky, b,rn2*ky, xs,ys*ky,rads, iDA
I = iDA.ksGetArrayCount


If i<> 0 then
iDA.ksGetArrayItem 0, iPoint
xp(0) = iPoint.x
if i = 1 then
      xp(1) = xp(0)
    else 
      iDA.ksGetArrayItem 1, iPoint
      xp(1) = iPoint.x
      if( xp(1)< xp(0) ) then
         x = xp(0)
        xp(0) = xp(1)
        xp(1) = x
      end if
    end if
    line 0,rn2*ky,xp(0),rn2*ky,1
    line xp(1),rn2*ky,b,rn2*ky,1
  else
    line 0,rn2*ky,b,rn2*ky,1
end if  
 
doc.ksArcByPoint  r,y3,r,0,y3,r,rn*ky,-ky,1
  line r,rn*ky,b-r,rn*ky,1
  doc.ksArcByPoint  b-r,y3,r,b-r,rn*ky,b,y3,-ky,1

Doc.kshatch  0, hatchAngle, hatchStep, 0, 0, 0
   line 0,rn2*ky,0,y3,1
   doc.ksArcByPoint r,y3,r,0,y3,r,rn*ky,-ky,1
   line r,rn*ky,b-r,rn*ky,1
   doc.ksArcByPoint b-r,y3,r,b-r,rn*ky,b,y3,-ky,1
   line b,y3,b,rn2*ky,1
 
    IF  I<>0  THEN

      line b,rn2*ky,xp(1),rn2*ky,1
      if  i>1 THEN
        doc.ksArcByPoint xs,ys*ky,rads,xp(1),rn2*ky,xp(0),rn2*ky,ky,1
      End if 
     line xp(0),rn2*ky,0,rn2*ky,1
 
    else
      line b,rn2*ky, 0,rn2*ky,1 
    end if

 Doc.ksEndObj

 If PAR_AXIS = 1 Then 
   line b,y3,b,0,1
   line 0,y3,0,0,1
  if  ky = 1 then
     line  -2, 0, b+2, 0, 3 
  end if
 End If 
 end sub 

'+++++
sub DrawInsideRing ( rv, rv2, b, r, xs,ys, rads, ky,  r1, rightSide ) 

if rightSide=1 then
rL = r
rR = r1
else
rL=r1
rR=r
end if

 yL=(rv+rL)*ky
 yR=(rv+rR)*ky 

  Dim  xp(2) 
  Dim  yp(2) 

  iDA.ksClearArray
math.ksIntersectLinLin  b*0.5, (ys-rads)*ky, -15*ky, b, 0, 90*ky, iDA
  I = iDA.ksGetArrayCount

 
If i<> 0 then
iDA.ksGetArrayItem 0, iPoint
 yp(0) = iPoint.y
 xp(0) = iPoint.x

if  rightSide =1  then
   yRight = yp(0)
   yLeft  = rv2*ky
 
 else 
   yLeft   = yp(0)
   yRight  = rv2*ky
 end if
end if

iDA.ksClearArray

if  rightSide = 1 then 
par =0
else 
par =b
end if


 math.ksIntersectLinSCir  par, rv2*ky, xs, rv2*ky, xs, ys*ky, rads, iDA

 I = iDA.ksGetArrayCount


 iDA.ksGetArrayItem 0, iPoint
 yp(0) = iPoint.y
 xp(0) = iPoint.x


  if rightSide=1 then 
    xp(1) = xs
    yp(1) = (ys-rads)*ky
  else 
    xp(1) = xp(0)
    yp(1) = yp(0)
    xp(0) = xs
    yp(0) = (ys-rads)*ky
end if  


   
   doc.ksArcByPoint b-rR,yR,rR,b,yR,b-rR,rv*ky,-ky,1
    line rL,rv*ky,b-rR,rv*ky,1
    doc.ksArcByPoint rL,yL,rL,rL,rv*ky,0,yL,-ky,1

  If PAR_AXIS = 1 Then  
   line rL,rv*ky,rL,0,1
   line b-rR,rv*ky,b-rR,0,1
  End If 
line  b, yRight, xp(1), yp(1), 1 
  line  xp(0), yp(0), 0, yLeft, 1


  Doc.kshatch  0, hatchAngle-90, hatchStep, 0, 0, 0
 

doc.ksArcByPoint b-rR,yR,rR,b,yR,b-rR,rv*ky,-ky,1
 line rL,rv*ky,b-rR,rv*ky,1
    doc.ksArcByPoint rL,yL,rL,rL,rv*ky,0,yL,-ky,1
 line 0,yL,0,yLeft,1
  doc.ksArcByPoint  xs, ys*ky, rads, xp(1), yp(1), xp(0), yp(0), - ky ,1
  line  b, yRight, xp(1), yp(1), 1 
 line  xp(0), yp(0), 0, yLeft, 1
  line b,yR,b,yRight,1


    Doc.ksEndObj

 end sub 

'++++++++
sub DrawSimple (rv, rn, rf1, rf2,rf3, rf4, b,  ky) 


 x1=rf1
 x2=rf2
 x3=b-rf3
 x4=b-rf4

 y1=(rv+rf1)*ky
 y2=(rn-rf2)*ky
 y3=(rn-rf3)*ky
 y4=(rv+rf4)*ky

    line 0,y1,0,y2,1

  doc.ksArcByPoint x2,y2,rf2,0,y2,x2,rn*ky,-ky,1 ';  /*5*/
  line x2,rn*ky,x3,rn*ky,1
  doc.ksArcByPoint x3,y3,rf3,x3,rn*ky,b,y3,-ky,1 ' ; /*6*/
  line b,y3,b,y4,1
doc.ksArcByPoint x4,y4,rf4,b,y4,x4,rv*ky,-ky,1 '; /*7*/
  line x4,rv*ky,x1,rv*ky,1
  doc.ksArcByPoint x1,y1,rf1,x1,rv*ky,0,y1,-ky,1 ';     /*8*/

If PAR_Simpl_AXIS = 1 Then 
  line x1,y1,x3,y3,2
  line x2,y2,x4,y4,2
Else 
 ''NEW 
   Line b/3,(rn+rv)*0.5*ky,2*b/3,(rn+rv)*0.5*ky,2
   Line b*0.5,((rn+rv)*0.5-b/6)*ky,b*0.5,((rn+rv)*0.5+b/6)*ky,2
   ''
End If 
  
  if PAR_AXIS = 1 THEN  
    line b,0,b,y4,1
    line x1,0,x1,rv*ky,1
    line x4,0,x4,rv*ky,1
    line 0,y1,0,0,1
     if ky = 1  then
      line -2, 0, b+2, 0, 3
     end if
  end if
end sub
'+++++++++++++++
'+++++



